﻿using Gx.Bll.Manage;
using Gx.Common;
using Gx.Dal.Manage;
using Gx.Model.Entity;
using Gx.Model.ViewModel;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Gx.ColdWarehouseErp.Controllers
{
    public class FinanceController : BaseController
    {
        protected LKCustomerPrePaymentBll lKCustomerPrePaymentBll = new LKCustomerPrePaymentBll();
        protected LKSettlementPaymentBll lKSettlementPaymentBll = new LKSettlementPaymentBll();
        #region 结算收款
        public ActionResult LKSettlementPaymentList()
        {
            return View();
        }

        /// <summary>
        /// 分页列表
        /// </summary>
        /// <param name="customerInput"></param>
        /// <returns></returns>
        public JsonResult GetLKSettlementPaymentPageList(LKSettlementPaymentInput input)
        {
            var result = lKSettlementPaymentBll.GetLKSettlementPaymentPageList(input);
            return JsonList(result.Total, result.List.ToArray());
        }


        /// <summary>
        /// 获取结算收款单
        /// </summary>
        /// <returns></returns>
        public JsonResult GetLKSettlementPayment()
        {
            var result = lKSettlementPaymentBll.GetLKSettlementPayments();
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 新增结算收款单
        /// </summary>
        /// <param name="lKSettlementPayment"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult AddLKSettlementPayment(LKSettlementPaymentDto lKSettlementPayment)
        {
            var result = lKSettlementPaymentBll.AddLKSettlementPayment(lKSettlementPayment);
            //string message = result ? "新增成功" : "新增失败";
            return Json(result.Success, result.Msg);
        }

        /// <summary>
        /// 编辑结算收款单
        /// </summary>
        /// <param name="lKSettlementPayment"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult EditLKSettlementPayment(LKSettlementPaymentDto lKSettlementPayment)
        {
            var result = lKSettlementPaymentBll.EditLKSettlementPayment(lKSettlementPayment);
            string message = result ? "编辑成功" : "编辑失败";
            return Json(result, message);
        }

        /// <summary>
        /// 删除结算收款单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public JsonResult DeleteLKSettlementPayment(int id)
        {
            var result = lKSettlementPaymentBll.DeleteLKSettlementPayment(id);
            //string message = result ? "删除成功" : "删除失败";
            //return Json(result, message);
            return Json(result.Success, result.Msg);
        }

        /// <summary>
        /// 添加结算收款单视图
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult AddLKSettlementPayment()
        {
            var bill = new LKSettlementPayment();
            bill.CreateUser = LoginHelper.UserName();
            bill.CreateUserId = LoginHelper.GetUserId();
            bill.CreateTime = DateTime.Now;
            bill.ModifyTime = DateTime.Now;
            bill.ModifyUserId = LoginHelper.GetUserId();
            bill.ModifyUser = LoginHelper.UserName();
            return View(bill);
        }

        /// <summary>
        /// 添加结算收款单分部视图
        /// </summary>
        /// <param name="domId"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult _AddLKSettlementPaymentDetail(int domId)
        {
            return PartialView(domId);
        }

        [HttpPost]
        public ActionResult _AddLKSettlementPaymentBcDetail(BcCostInput input)
        {
            var result = lKSettlementPaymentBll.GetLKSettlementPaymentDetailListByGoodsNo(input.GoodsNo, input.CustomerId);
            var cost = new BcCostDto { DomId = input.DomId,SettlementPaymentDetail=result };
            return PartialView(cost);
        }

        /// <summary>
        /// 获取客户待收费明细
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public JsonResult GetLKSettlementPaymentDetailListByCustomer(SelectCostInput input)
        {
            var result = lKSettlementPaymentBll.GetLKSettlementPaymentDetailListByCustomer(input);
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 编辑结算收款单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult EditLKSettlementPayment(int id)
        {
            var result = lKSettlementPaymentBll.GetLKSettlementPaymentById(id);
            return View(result);
        }

        [HttpPost]
        public ActionResult _CostTotal()
        {
            return PartialView();
        }

        /// <summary>
        /// 复核
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult CheckLKSettlementPayment(int id)
        {
            var result = lKSettlementPaymentBll.ReviewLKSettlementPayment(id);
            string message = result ? "复核成功" : "复核失败";
            return Json(result, message);
        }

        [HttpPost]
        public JsonResult GetCustomerPreAmount(GetGoodOutDetailInput input)
        {
            var result = lKSettlementPaymentBll.GetPreAmount(input.CustomerId);
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        //打印结算收款单
        [HttpGet]
        public ActionResult LKSettlementPaymentPrint(int id)
        {
            var result = lKSettlementPaymentBll.GetLKSettlementPaymentById(id);
            return View(result);
        }
        #endregion

        #region 客户预付款
        public ActionResult LKCustomerPrePaymentList()
        {
            return View();
        }

        /// <summary>
        /// 分页列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public JsonResult GetLKCustomerPrePaymentPageList(LKCustomerPrePaymentInput input)
        {
            var result = lKCustomerPrePaymentBll.GetLKCustomerPrePaymentPageList(input);
            return JsonList(result.Total, result.List.ToArray());
        }

        /// <summary>
        /// 添加客户预付款视图
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult AddLKCustomerPrePayment()
        {
            var bill = new LKCustomerPrePayment();
            bill.PrePayTime = DateTime.Now;
            bill.CreateUser = LoginHelper.UserName();
            bill.CreateUserId = LoginHelper.GetUserId();
            bill.CreateTime = DateTime.Now;
            bill.ModifyTime = DateTime.Now;
            bill.ModifyUserId = LoginHelper.GetUserId();
            bill.ModifyUser = LoginHelper.UserName();
            return View(bill);
        }

        /// <summary>
        /// 新增客户预付款
        /// </summary>
        /// <param name="lKCustomerPrePayment"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult AddLKCustomerPrePayment(LKCustomerPrePayment lKCustomerPrePayment)
        {
            var result = lKCustomerPrePaymentBll.AddLKCustomerPrePayment(lKCustomerPrePayment);
            //string message = result ? "新增成功" : "新增失败";
            return Json(result.Success, result.Msg);
        }


        /// <summary>
        /// 编辑客户预付款
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult EditLKCustomerPrePayment(int id)
        {
            var result = lKCustomerPrePaymentBll.GetLKCustomerPrePaymentById(id);
            return View(result);
        }

        /// <summary>
        /// 编辑客户预付款
        /// </summary>
        /// <param name="lKCustomerPrePayment"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult EditLKCustomerPrePayment(LKCustomerPrePayment lKCustomerPrePayment)
        {
            var result = lKCustomerPrePaymentBll.EditLKCustomerPrePayment(lKCustomerPrePayment);
            string message = result ? "编辑成功" : "编辑失败";
            return Json(result, message);
        }

        /// <summary>
        /// 删除客户预付款
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public JsonResult DeleteLKCustomerPrePayment(int id)
        {
            var result = lKCustomerPrePaymentBll.DeleteLKCustomerPrePayment(id);
            string message = result ? "删除成功" : "删除失败";
            return Json(result, message);
        }
        #endregion
    }
}